package com.abc.cloud.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.abc.cloud.common.utils.UserHolder;
import com.abc.cloud.dto.notice.PageDto;
import com.abc.cloud.vo.notice.NoticeAddVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.abc.cloud.entity.NoticeEntity;
import com.abc.cloud.service.NoticeService;
import com.abc.cloud.common.utils.PageUtil;
import com.abc.cloud.common.utils.Result;


/**
 * 通知表
 *
 * @author LiJunXi
 * @email 2770063826@qq.com
 * @date 2023-11-29 13:19:41
 */


@Slf4j
@Api(tags = "用户消息通知")
@RestController
@RequestMapping("/notice")
public class NoticeController {
    @Autowired
    private NoticeService noticeService;


    /**
     * 列表
     */
    @RequestMapping("/list")
    public Result list(@RequestParam Map<String, Object> params) {
        PageUtil page = noticeService.queryPage(params);

        return Result.success(page, "page");
    }

    /**
     * 列表2
     */
    @ApiOperation(value = "用户消息通知列表")
    @GetMapping("/getList")
    public Result getList(@RequestParam Map<String, Object> params) {
        PageDto page = noticeService.getList(params);
        return Result.success(page, "page");
    }

    //    后台管理
    @ApiOperation(value = "后台通知管理")
    @GetMapping("/admin/getAllList")
    public Result getAllList(@RequestParam Map<String, Object> params) {
        PageUtil page = noticeService.getAllList(params);
        return Result.success(page, "page");
    }

    //    新增通知
    @ApiOperation(value = "新增通知")
    @PostMapping("/admin/addNotice")
    public Result addNotice(@RequestBody NoticeAddVo noticeAddVo) {
        Long userId = UserHolder.getUser().getId();
        String username = UserHolder.getUser().getUsername();
        if (noticeService.addNotice(userId, username, noticeAddVo.getTitle(), noticeAddVo.getContent(), noticeAddVo.getTargetUsername(), noticeAddVo.getReceiverType(), noticeAddVo.getType())) {
            return Result.success(null, "success");
        }
        return Result.fail(null, "添加失败");
    }

    @ApiOperation(value = "修改通知信息")
    @PutMapping("/admin/updateNotice")
    public Result updateNotice(@RequestBody NoticeEntity notice) {
        log.info("-------------{}", notice);
        noticeService.updateById(notice);
        return Result.success(null, "success");
    }

    //管理员修改 NoticeStatus
    @ApiOperation(value = "修改通知状态")
    @PutMapping("/admin/updateStatue/{status}")
    public Result adminUpdateUserStatus(@RequestBody List<Long> noticeIds,
                                        @PathVariable("status") Integer status) {
        noticeService.updateNoticeStatus(noticeIds, status);
        return Result.success(null, "修改成功");
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public Result info(@PathVariable("id") Long id) {
        NoticeEntity notice = noticeService.getById(id);

        return Result.success(notice, "notice");
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public Result save(@RequestBody NoticeEntity notice) {
        noticeService.save(notice);

        return Result.success(null, "");
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public Result update(@RequestBody NoticeEntity notice) {
        noticeService.updateById(notice);

        return Result.success(null, "");
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public Result delete(@RequestBody Long[] ids) {
        noticeService.removeByIds(Arrays.asList(ids));

        return Result.success(null, "");
    }

}
